/*******************************************************************************
 * Copyright 2008 www.softao.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.softao.ming.gwt.user.client;

import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Widget;

/**
 * Defines implementations of the {@link WidgetReader}.
 * 
 * @author Dop Sun Jul 15, 2008
 */
public final class WidgetReaders {
	private static IntegerReader mIntegerReader;

	private static LongReader mLongReader;

	private static FloatReader mFloatReader;

	private static DoubleReader mDoubleReader;

	private static StringReader mStringReader;

	private static BooleanReader mBooleanReader;

	/**
	 * @return the default implementation for reading integer value
	 */
	public synchronized static final IntegerReader getIntegerReader() {
		if (mIntegerReader == null) {
			mIntegerReader = new IntegerReader();
		}

		return mIntegerReader;
	}

	/**
	 * @return the default implementation for reading long value
	 */
	public synchronized static final LongReader getLongReader() {
		if (mLongReader == null) {
			mLongReader = new LongReader();
		}

		return mLongReader;
	}

	/**
	 * @return the default implementation for reading float value
	 */
	public synchronized static final FloatReader getFloatReader() {
		if (mFloatReader == null) {
			mFloatReader = new FloatReader();
		}

		return mFloatReader;
	}

	/**
	 * @return the default implementation for reading double value
	 */
	public synchronized static final DoubleReader getDoubleReader() {
		if (mDoubleReader == null) {
			mDoubleReader = new DoubleReader();
		}

		return mDoubleReader;
	}

	/**
	 * @return the default implementation for reading string value
	 */
	public synchronized static final StringReader getStringReader() {
		if (mStringReader == null) {
			mStringReader = new StringReader();
		}

		return mStringReader;
	}

	/**
	 * @return the default implementation for reading boolean value
	 */
	public synchronized static final BooleanReader getBooleanReader() {
		if (mBooleanReader == null) {
			mBooleanReader = new BooleanReader();
		}

		return mBooleanReader;
	}

	/**
	 * Reads integer value from the widget. default is zero.
	 * 
	 * @author Dop Sun Jul 15, 2008
	 */
	public static class IntegerReader implements WidgetReader<Integer> {
		public Integer read(Widget widget) {
			if (widget instanceof HasText) {
				HasText target = (HasText) widget;
				String text = target.getText();
				return Integer.parseInt(text);
			}
			return 0;
		}
	}

	/**
	 * Reads double value from the widget. default is zero.
	 * 
	 * @author Dop Sun Jul 15, 2008
	 */
	public static class DoubleReader implements WidgetReader<Double> {
		public Double read(Widget widget) {
			if (widget instanceof HasText) {
				HasText target = (HasText) widget;
				String text = target.getText();
				return Double.parseDouble(text);
			}
			return 0D;
		}
	}

	/**
	 * Reads float value from the widget. default is zero.
	 * 
	 * @author Dop Sun Jul 15, 2008
	 */
	public static class FloatReader implements WidgetReader<Float> {
		public Float read(Widget widget) {
			if (widget instanceof HasText) {
				HasText target = (HasText) widget;
				String text = target.getText();
				return Float.parseFloat(text);
			}
			return 0F;
		}
	}

	/**
	 * Reads long value from the widget. Default is zero.
	 * 
	 * @author Dop Sun Jul 15, 2008
	 */
	public static class LongReader implements WidgetReader<Long> {
		public Long read(Widget widget) {
			if (widget instanceof HasText) {
				HasText target = (HasText) widget;
				String text = target.getText();
				return Long.parseLong(text);
			}
			return 0L;
		}
	}

	/**
	 * Reads String value from the widget. Default is <code>null</code>.
	 * 
	 * @author Dop Sun Jul 15, 2008
	 */
	public static class StringReader implements WidgetReader<String> {
		public String read(Widget widget) {
			if (widget instanceof HasText) {
				HasText target = (HasText) widget;
				String text = target.getText();
				return text;
			}
			return null;
		}
	}

	/**
	 * Reads String value from the widget. Default is <code>false</code>.
	 * 
	 * @author Dop Sun Jul 15, 2008
	 */
	public static class BooleanReader implements WidgetReader<Boolean> {
		public Boolean read(Widget widget) {
			if (widget instanceof CheckBox) {
				CheckBox target = (CheckBox) widget;
				return target.isChecked();
			}
			return false;
		}
	}

}
